package com.itheima02;

import com.itheima.case2.service.RoleService;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

public class ServiceFactory {
    //容器: 存了所有的key和对象的映射关系
    private static HashMap<String,Object> map = new HashMap<>();
    static{
        try {
            InputStream is = ServiceFactory.class.getClassLoader().getResourceAsStream("service.properties");
            Properties p = new Properties();
            p.load(is);
            //获取Properties文件中所有的key
            Set<String> keySet = p.stringPropertyNames();
            //[roleService, userService]
//            System.out.println(keySet);
            for (String key : keySet) {
                String className = p.getProperty(key);
                Object obj = Class.forName(className).newInstance();
                map.put(key,obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public  Object getBean(String serviceName) throws Exception {

        return map.get(serviceName);
    }
}
